package com.itheima.controller;

import com.itheima.client.UserClient;
import com.itheima.domian.User;
import com.netflix.hystrix.contrib.javanica.annotation.DefaultProperties;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.Date;



@DefaultProperties(defaultFallback = "defaultFallback")
@RestController
public class ConsumerController {

    @Autowired
    private UserClient userClient;
    @Autowired  // 注入RestTemplate模板工具
    private RestTemplate restTemplate;
    @Autowired
    DiscoveryClient discoveryClient;



    /*@HystrixCommand(fallbackMethod = "findUserByIdFallbackMethod")*/
    @HystrixCommand
    @GetMapping("/consumer/user/{id}")
    public User getUserById(@PathVariable("id") Long id) {

        User user = userClient.queryUserById(id);
        return user;
    }
    /**
     * 针对某个接口提供服务降级方法
     * 条件：
     * 1.方法返回值必须跟接口放回值一样
     * 2.方法参数必须跟接口参数一样
     */
    public User findUserByIdFallbackMethod(Long id){
        return new User(null, "无法调用服务，请稍后再试！", null, new Date());
    }

    /**
     * 全局服务降级方法
     * 条件：
     * 方法返回值必须跟controller所有接口放回值一样
     */
    public User defaultFallback() {
        return new User(null, "无法调用服务，全局服务降级，请稍后再试！", null, new Date());
    }
}
